﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WISD.CarFriend.Model;
using WISD.Common;
using System.Data.Objects;
using System.Data;
using WISD.DBUtility.DBOperate;

namespace WISD.CarFriend.BLL.OA
{
    public class WebModule
    {
        #region 用户登录
        /// <summary>
        /// 验证 登录 是否存在
        /// </summary>
        /// <param name="user"></param>
        public static bool ValidLogin(string userName, string userPassword, out string memberID)
        {
            memberID = string.Empty;
            MemberModule memberModule = new MemberModule();
            DataSet ds = memberModule.GetList(" WebUserName='" + userName + "' and WebPwd='" + userPassword + "' ");
            if (ds.Tables[0].Rows.Count > 0)
            {
                memberID = ds.Tables[0].Rows[0]["MemberID"].ToString();
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 注册会员
        /// <summary>
        /// 保存注册用户
        /// </summary>
        /// <param name="member"></param>
        public void AddRegisterMember(T_CF_OA_Member member)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                member.IsCard = 0;
                member.State = 1;
                member.ReviseDate = DateTime.Now;

                edm.T_CF_OA_Member.AddObject(member);
                edm.SaveChanges();
            }
        }

        /// <summary>
        /// 验证会员id重复
        /// </summary>
        /// <param name="id"></param>
        public bool ValidMemberID(string id)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return edm.T_CF_OA_Member.FirstOrDefault(p => p.MemberID == id) != null;
            }
        }

        /// <summary>
        /// 验证会员用户重复
        /// </summary>
        /// <param name="id"></param>
        public bool ValidWebUserName(string userName)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return edm.T_CF_OA_Member.FirstOrDefault(p => p.WebUserName == userName) != null;
            }
        }

        /// <summary>
        /// 根据用户ID获取 用户信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public T_CF_OA_Member GetUser(string userName, string PWD)
        {
            T_CF_OA_Member user = new T_CF_OA_Member();

            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                user = edm.T_CF_OA_Member.FirstOrDefault(p => p.Name == userName
                    && p.WebPwd == PWD);
            }
            return user;
        }
        #endregion

        #region 会员开卡

        /// <summary>
        /// 会员开卡
        /// </summary>
        /// <param name="MemberCard"></param>
        public void AddMemberCard(T_CF_OA_MemberCard MemberCard)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                MemberCard.Riviser = "1";
                MemberCard.AdmissionDate = DateTime.Now;   //入会日期
                MemberCard.ExpirationDate = DateTime.Now;   //到期日期
                MemberCard.RiviseDate = DateTime.Now;   //最后修改日期
                edm.T_CF_OA_MemberCard.AddObject(MemberCard);
                edm.SaveChanges();
            }
        }

        /// <summary>
        /// 验证会员卡id重复
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ValidMemberCardID(string id)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return edm.T_CF_OA_MemberCard.FirstOrDefault(p => p.MemberCardID == id) != null;
            }
        }
        #endregion

        #region 酒后代驾

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="MemberCard"></param>
        public void AddMemberCard(T_CF_OA_SubstituteDriving SubstituteDriving)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                SubstituteDriving.Reviser = "1";
                SubstituteDriving.ReviseDate = DateTime.Now;
                SubstituteDriving.Memo3 = StringHandle.GetRamCode().Substring(0, 14);//编号为自动生成14位的时间值
                edm.T_CF_OA_SubstituteDriving.AddObject(SubstituteDriving);
                edm.SaveChanges();
            }
        }

        /// <summary>
        /// 验证会员名称重复
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ValidSubstituteDrivingName(string name)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return edm.T_CF_OA_SubstituteDriving.FirstOrDefault(p => p.Name == name) != null;
            }
        }
        #endregion

        #region 消费查询
        /// <summary>
        /// 获取会员消费记录信息
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public static DataSet GetMemberConsumeItems(string memberId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT     a.ConsumeID, a.MemberCardID, a.ConsumeTime, a.ShopName, a.ShopLeader, a.ConsumeLocation, a.ConsumeIDItem, a.ConsumeAmount, a.PaidAmount,  ");
            strSql.Append(" b.MemberID, b.ResidualAmount, c.ConsumeItem, c.ConsumeCount, c.RemainConsumeCount ");
            strSql.Append(" FROM   T_CF_OA_Consume AS a INNER JOIN ");
            strSql.Append(" T_CF_OA_MemberCard AS b ON a.MemberCardID = b.MemberCardID INNER JOIN ");
            strSql.Append(" T_CF_OA_MemberConsumeItem AS c ON a.ConsumeIDItem = c.Id ");
            strSql.Append(" WHERE     (b.MemberID = '" + memberId.Trim() + "') ");
            strSql.Append(" ORDER BY a.ConsumeTime DESC ");

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion

        #region 充值查询
        /// <summary>
        /// 获取会员充值记录信息
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public static DataSet GetMemberRechargeItems(string memberId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT     a.MemberCardID, a.RechargeAmount, a.PaidAmount, a.Reviser, a.ReviseDate, b.MemberID, b.ResidualAmount ");
            strSql.Append(" FROM         T_CF_OA_Recharge AS a INNER JOIN ");
            strSql.Append(" T_CF_OA_MemberCard AS b ON a.MemberCardID = b.MemberCardID ");
            strSql.Append(" WHERE     (b.MemberID = '" + memberId.Trim() + "') ");
            strSql.Append(" ORDER BY a.ReviseDate DESC ");

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
    }
}
